package main

import (
	"fmt"
	"sync"
)

var wg4 sync.WaitGroup
var lock sync.Mutex
var total4 int

func add4() {
	defer wg4.Done()
	for i := 0; i < 1000000; i++ {
		lock.Lock()
		total4 = total4 + 1
		lock.Unlock()
	}
}

func sub4() {
	defer wg4.Done()
	for i := 0; i < 1000000; i++ {
		lock.Lock()
		total4 = total4 - 1
		lock.Unlock()
	}
}

func main() {
	wg4.Add(2)
	go add4()
	go sub4()
	wg4.Wait()
	fmt.Println("total 4 =", total4)
}
